package com.example.soscall.utils;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.soscall.bean.WeatherBean;
import com.example.soscall.callback.NotifyListener;
import com.google.gson.Gson;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

public class CommonUtil {

    public static boolean isAndroidNativeLightStatusBar(Activity activity) {
        View decor = activity.getWindow().getDecorView();
        return (View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR & decor.getSystemUiVisibility()) != 0;
    }

    public static void setTranslucentStatusBarAndNavigationBar(Activity activity) {
        Window window = activity.getWindow();
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                //| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        );
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(Color.TRANSPARENT);
            //window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {

            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    static String responseText = "";

    public static Disposable getWeather(String location, NotifyListener listener) throws NoSuchAlgorithmException, InvalidKeyException, InterruptedException {
        String urlStr = SeniverseUtils.getInstance().getUrl(location);
        responseText = "";
        final CountDownLatch latch = new CountDownLatch(1);
        Disposable subscribe = Flowable.create((emitter) -> {
            String jsonStringContent = HttpUtils.getJsonStringContent(urlStr);
            responseText = jsonStringContent;
            emitter.onNext(responseText);
        }, BackpressureStrategy.DROP).compose(RxUtils.io2main()).subscribe(s -> {
            latch.countDown();
        }, Throwable::printStackTrace);
        latch.await(500, TimeUnit.MILLISECONDS);
        if (!TextUtils.isEmpty(responseText)) {
            WeatherBean weatherBean = new Gson().fromJson(responseText, WeatherBean.class);
            String name = weatherBean.getResults().get(0).getLocation().getName();
            String text = weatherBean.getResults().get(0).getNow().getText();
            listener.onNotify(name + " " + text, null);
        } else {
            listener.onNotify(null, null);
        }
        return subscribe;
    }
}
